#!/bin/sh
#
# Provisioning script for Stratio Sandbox.
# More info at https://github.com/Stratio/sandbox
#
#

##############
## SERVICES ##
##############

echo -e 'Installing Stratio Decision services...'

yum -y -q --nogpgcheck install httpd stratio-decision stratio-decision-shell

chkconfig zookeeper off
chkconfig kafka off
chkconfig decision off
chkconfig cassandra on
chkconfig elasticsearch on
chkconfig httpd on

cat >/etc/rc.local <<EOF
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

service zookeeper start
sleep 5
service kafka start
sleep 5
service decision start

touch /var/lock/subsys/local

EOF

if ! grep -R "analyzer.default.type" "/etc/sds/elasticsearch/elasticsearch.yml"; then
    echo "index.analysis.analyzer.default.type: keyword" >> /etc/sds/elasticsearch/elasticsearch.yml
fi

service zookeeper restart
service kafka restart
service cassandra restart
service mongodb restart
service elasticsearch restart
service decision restart
service httpd restart

############
## KIBANA ##
############

if [ ! -d "/var/www/html/kibana" ]; then
    echo -e 'Downloading kibana...'
	wget -q 'https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz' -P /home/vagrant/downloads
	
	echo -e 'Uncompressing kibana...'
	tar -xzf /home/vagrant/downloads/kibana-3.1.0.tar.gz -C /var/www/html
	mv /var/www/html/kibana-3.1.0 /var/www/html/kibana
fi

################################
## STRATIO DECISION EXAMPLES ##
################################

DOWNLOAD_EXAMPLES_URL="https://s3.amazonaws.com/stratioorg/decision-examples-${STRATIO_MODULE_VERSION}-app.tar.gz"

echo $DOWNLOAD_EXAMPLES_URL

if [ ! -d "/opt/sds/decision-examples" ]; then
	echo -e 'Building Stratio Decision Examples...'
	mkdir /opt/sds/decision-examples
	wget -q $DOWNLOAD_EXAMPLES_URL -P /home/vagrant/downloads
	tar -xzf /home/vagrant/downloads/decision-examples-$STRATIO_MODULE_VERSION-app.tar.gz -C /home/vagrant/downloads
	cp -fr /home/vagrant/downloads/decision-examples-$STRATIO_MODULE_VERSION/* /opt/sds/decision-examples
fi

#######################
## KIBANA DASHBOARDS ##
#######################

cp -f /opt/sds/decision-examples/dashboards/*.json /var/www/html/kibana/app/dashboards
chmod -R 777 /var/www/html/kibana/app/dashboards

#####################
## WELCOME MESSAGE ##
#####################
